package com.kitchen.ui.controls.friends
{
	import com.kitchen.AbstractIcon;
	import com.kitchen.ui.controls.Portrait;
	import com.kitchen.ui.controls.friends.button.FlowerButton;
	import com.kitchen.ui.controls.friends.button.HamburButton;
	import com.kitchen.ui.controls.friends.button.IceButton;
	import com.kitchen.ui.controls.friends.icon.ChainIcon;
	import com.kitchen.ui.controls.friends.icon.EmployIcon;
	import com.kitchen.ui.controls.friends.icon.ListItemBg;
	import com.kitchen.ui.controls.friends.icon.WorkIcon;
	
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	internal class ListItem extends Sprite
	{
		private var bg:ListItemBg;
		public var h:Number;
		public var w:Number;
		public var data:Object;
		
		public function ListItem(mainSkin:BitmapData, friend:Object)
		{
			super();
			this.layout(mainSkin, friend);
			this.addEventListener(MouseEvent.MOUSE_OVER, overHandle);
			this.addEventListener(MouseEvent.MOUSE_OUT, outHandle);
		}
		
		private function layout(mainSkin:BitmapData, friend:Object):void{
			this.w = 145;
			this.h = 45;
			this.data = friend;
			//background
			this.bg = new ListItemBg(mainSkin);
			this.addChild(this.bg);
			
			//photo
			var photo:Portrait = new Portrait(friend.photo, 35, 35);
			photo.name = 'photo';
			photo.addEventListener(MouseEvent.CLICK, clickHandle);
			photo.x = 5;
			photo.y = 5;
			this.addChild(photo);
			
			//title
			var tf:TextField = new TextField();
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.text = String(friend.nick).substr(0, 6);
			var tff:TextFormat = new TextFormat();
			tff.font = '黑体';
			tff.size = 12;
			tff.color = 0xb55620;
			tff.bold = true;
			tf.setTextFormat(tff);
			tf.x = 45;
			tf.y = 2;
			tf.selectable = false;
			tf.defaultTextFormat = tff;
			this.addChild(tf);
			
			//rank num
			var color:uint = 0;
			var fcolor:uint = 0xfefefc;
			var num:int = int(friend.odnum);
			if(num > 3){
				color = 0xfdd26a;
				fcolor = 0xffbd30;
			}else if(num == 1){
				color = 0xf6878b;
			}else if(num == 2){
				color = 0xedb44d;
			}else if(num == 3){
				color = 0x22bd48;
			}
			if(4 > num){
				var circle:Shape = new Shape();
				circle.graphics.beginFill(color,1);
				circle.graphics.drawCircle(0, 0, 9);
				circle.y = 33;
				circle.x = 12;
				this.addChild(circle);
			}
			var ntf:TextField = new TextField();
			ntf.text = String(num);
			ntf.autoSize = TextFieldAutoSize.LEFT;
			var ntff:TextFormat = new TextFormat();
			ntff.color = fcolor;
			ntff.bold = true;
			ntff.size = 15;
			ntff.font = '黑体';
			ntf.setTextFormat(ntff);
			ntf.defaultTextFormat = ntff;
			ntf.y = 23;
			ntf.selectable = false;
			ntf.x = 5;
			this.addChild(ntf);
			//shop type
			var kitchen:Array = String(friend.kitchen).split(',');
			//trace(kitchen);
			var len:int = kitchen.length;
			len = len>3?3:len;
			if(len > 0){
				for(var i:int=0; i<len; i++){
					if(int(kitchen[i]) == 1){
						var hamb:HamburButton = new HamburButton(mainSkin);
						hamb.name = 'hambo';
						hamb.addEventListener(MouseEvent.CLICK, clickHandle);
						hamb.y = 18;
						hamb.x = 42+i*25;
						this.addChild(hamb);
					}else if(int(kitchen[i]) == 2){
						var ice:IceButton = new IceButton(mainSkin);
						ice.name = 'ice';
						ice.addEventListener(MouseEvent.CLICK, clickHandle);
						ice.y = 18;
						ice.x = 42+i*25;
						this.addChild(ice);
					}else if(int(kitchen[i]) == 3){
						var flower:FlowerButton = new FlowerButton(mainSkin);
						flower.name = 'flower';
						flower.addEventListener(MouseEvent.CLICK, clickHandle);
						flower.y = 18;
						flower.x = 42+i*25;
						this.addChild(flower);
					}
				}
			}
			//relationship
			var rel:int = int(friend.relate);
			var relIcon:AbstractIcon = null;
			if(rel == 1){
				relIcon = new ChainIcon(mainSkin) as AbstractIcon;
			}else if(rel == 2){
				relIcon = new EmployIcon(mainSkin) as AbstractIcon;
			}else if(rel ==0){
				relIcon = new WorkIcon(mainSkin) as AbstractIcon;
			}
			if(relIcon != null){
				relIcon.x = 116;
				relIcon.y = 18;
				this.addChild(relIcon);
			}
		}
		
		private function overHandle(me:MouseEvent):void{
			this.bg.focus();
		}
		
		private function outHandle(me:MouseEvent):void{
			this.bg.unfocus();
		}
		
		private function clickHandle(me:MouseEvent):void{
			this.dispatchEvent(me);
		}
	}
}